package io.gitee.kotle.player.ui.view.exo

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import androidx.media3.common.MediaItem
import io.gitee.kotle.player.core.IPlayer
import io.gitee.kotle.player.core.impl.exoplayer.IExoPlayerMode

/**
 * 播放出错的view
 */
class ExoPlayerErrorView : AppCompatTextView, IExoPlayerView {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun needPlayerListener(): Boolean {
        return true
    }

    override fun onError(player: IPlayer<MediaItem, IExoPlayerMode>, exception: Throwable) {
        super.onError(player, exception)
        text = findCause(exception).message
    }

    private fun findCause(exception: Throwable): Throwable {
        val next = exception.cause
        return if (next == null) {
            exception
        } else {
            findCause(next)
        }
    }
}